package cn.itcast.util;

import org.springframework.data.domain.Page;

import java.util.List;

/**
 * @author : lyy
 * @version V1.0
 * @Project: Common
 * @Package com.xianxian.basic.entity
 * @Description: TODO
 * @date Date : 2018年11月12日 15:05
 **/
public class ResponseEntity<E> extends BasicEntity {
    private int code;
    private String message;
    private E entity;
    private List<E> data;
    private Long total;
    private Integer totalPages;
    private String status;
    private Object result;
    public ResponseEntity(){}
    public ResponseEntity(List<E> data,Long total){
            this.code=200;
        this.data=data;
        this.total=total;
    }
    public ResponseEntity(E e){
        this.code=300;
        if(e!=null) {
            this.entity = e;
            this.code=200;
        }
    }

    public ResponseEntity(List list){
        this.data=list;
        this.code=200;
    }
    public ResponseEntity(Page<E> page){
        this.data=page.getContent();
        this.total=page.getTotalElements();
        this.totalPages=page.getTotalPages();
        this.code=200;
    }
    public ResponseEntity(int code){this.code=code;}
    public ResponseEntity(int code,String message){
        this.code=code;
        this.message=message;
    }
    public ResponseEntity(int code,String message,E entity){
        this.code=code;
        this.message=message;
        this.entity=entity;
    }
    public ResponseEntity(int code,String message,List<E> data){
        this.code=code;
        this.message=message;
        this.data=data;
    }
    public ResponseEntity(int code,String message,E entity,List<E> data){
        this.code=code;
        this.message=message;
        this.data=data;
        this.entity=entity;
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public E getEntity() {
        return entity;
    }

    public void setEntity(E entity) {
        this.entity = entity;
    }

    public List<E> getData() {
        return data;
    }

    public void setData(List<E> data) {
        this.data = data;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public ResponseEntity<E> error_300(){
        this.code= ResponseCode.SC_PARAMETER_ERROR;
        this.message="参数错误";
        return this;
    }
    public ResponseEntity<E> error(String message){
        this.code=ResponseCode.SC_PARAMETER_ERROR;
        this.message=message;
        return this;
    }
    public ResponseEntity<E> success(String message){
        this.code=ResponseCode.SC_SUCCESS;
        if(!TextUtils.isEmpty(message))
            this.message=message;
        else this.message="提交成功";
        return this;
    }

    public boolean isSuccess(){
        return this.code==200;
    }

    public Integer getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(Integer totalPages) {
        this.totalPages = totalPages;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }
}
